package com.xry.match.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xry.common.bean.Stadium;
import com.xry.common.service.StadiumService;
import com.xry.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stadium")
public class StadiumController {
    @Autowired
    StadiumService stadiumService;
    @GetMapping("{id}")
    public Result getStadiumById(@PathVariable("id") Integer id) {
        try{
            Object obj=stadiumService.getStadiumById(id);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @PostMapping("/addStadium")
    public Result addStadium(@RequestBody Stadium stadium) {
        try{
            stadiumService.addStadium(stadium);
            stadium = stadiumService.getStadiumById(stadium.getId());
            return Result.success("新增成功", stadium);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @PutMapping("/updateStadiumById")
    public Result updateStadiumById(@RequestBody Stadium stadium) {
        try{
            boolean flag=stadiumService.updateStadiumById(stadium);
            if(flag){
                return Result.success("修改成功", stadium);
            }else{
                return Result.fail(500,"修改失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @DeleteMapping("{id}")
    public Result deleteStadiumById(@PathVariable("id") Integer id) {
        try{
            boolean flag=stadiumService.deleteStadiumById(id);
            if(flag){
                return Result.success("删除成功");
            }else{
                return Result.fail(500,"删除失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getAll")
    public Result getPage() {
        try{
            Object obj=stadiumService.getAll();
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getPage")
    public Result getPage(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "10") Integer size,
                          Stadium stadium) {
        try{
            // 创建分页对象
            IPage<Stadium> page = new Page<>(current, size);
            Object obj = stadiumService.getPage(stadium, page);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }


}
